package com.xiaodai.miaosha.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.corba.se.impl.activation.RepositoryImpl;
import com.xiaodai.miaosha.entity.Application;
import com.xiaodai.miaosha.entity.Msuser;
import com.xiaodai.miaosha.entity.Screening;
import com.xiaodai.miaosha.service.IApplicationService;
import com.xiaodai.miaosha.service.IMsuserService;
import com.xiaodai.miaosha.service.IScreeningService;
import com.xiaodai.miaosha.vo.RespBean;
import com.xiaodai.miaosha.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaodai
 * @since 2022-04-12
 */
@Controller
@RequestMapping("/application")
public class ApplicationController {
    @Autowired
    private IApplicationService applicationService;
    @Autowired
    private IScreeningService screeningService;
    @Autowired
    private IMsuserService userService;

    @GetMapping("/doApplication")
    @ResponseBody
    public RespBean doApplication(@CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response,Long goodsId){
        Msuser Msuser = userService.getUserByCookie(ticket,request,response);
        if (Msuser == null){
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
//        screeningService.doScreening(Msuser);
//        Screening screening = screeningService.getOne(new QueryWrapper<Screening>().eq("msuser_id", Msuser.getId()));
//        if (screening.getPassed()==0){
//            return RespBean.error(RespBeanEnum.SCREENING_UNPASSED);
//        }
        Application one = applicationService.getOne(new QueryWrapper<Application>().eq("msuser_id", Msuser.getId()).eq("goods_id", goodsId));
        if (one != null){
            if (one.getPassed()==1){
                return RespBean.success("申请已通过");
            }else {
                return RespBean.success("申请待通过");
            }
        }
        Application application = new Application();
        application.setMsuserId(String.valueOf(Msuser.getId()));
        application.setGoodsId(String.valueOf(goodsId));
        application.setPassed(0);
        applicationService.save(application);
        return RespBean.success("申请已提交");
    }

    @GetMapping("/check")
    @ResponseBody
    public RespBean check(@CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response,Long goodsId){
        Msuser Msuser = userService.getUserByCookie(ticket,request,response);
        if (Msuser == null){
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        Application one = applicationService.getOne(new QueryWrapper<Application>().eq("msuser_id", Msuser.getId()).eq("goods_id", goodsId));
        if (one==null){
            return RespBean.error(RespBeanEnum.NO_APPLICATION);
        }
        if (one.getPassed()==1){
            return RespBean.success();
        }else if (one.getPassed()==2){
            return RespBean.error(RespBeanEnum.APPLICATION_NOT_PASSED);
        }else {
            return RespBean.error(RespBeanEnum.APPLICATION_NOT_CHECK);
        }

    }

}
